﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if !WINDOWS_PHONE
using Microsoft.Xna.Framework.Storage;
#endif
using Microsoft.Xna.Framework.GamerServices;

namespace Brain
{
    public class StorageDeviceManager : IUpdateable, IStorageDeviceManager
    {
        public bool ShouldUpdate { get; set; }

#if !WINDOWS_PHONE
        public StorageDevice StorageDevice { get; protected set; }
#endif

        /// <summary>
        /// If the Storage Device was sucessfully loaded
        /// </summary>
        public bool DeviceLoaded { get; protected set; }

        /// <summary>
        /// If this Manager is waiting for an ASynchronous Result
        /// </summary>
        public bool InActivity { get; protected set; }

        /// <summary>
        /// The Free space in kbytes
        /// </summary>
        public float FreeSpace_KBytes;

        /// <summary>
        /// The Free space in mbytes
        /// </summary>
        public float FreeSpace_MBytes;

        /// <summary>
        /// The Free space in gbytes
        /// </summary>
        public float FreeSpace_GBytes;

        /// <summary>
        /// If the User chosen to cancel the file operation, and didn't
        /// select a storage device
        /// </summary>
        public bool UserChosenNotToUseStorage { get; protected set; }


        public StorageDeviceManager()
        {
            ShouldUpdate = true;
        }

        public void Reload()
        {
        }

        public void Update()
        {
#if !WINDOWS_PHONE
            if (!UserChosenNotToUseStorage && !DeviceLoaded && !InActivity && !Guide.IsVisible)
            {
                InActivity = true;
                StorageDevice.BeginShowSelector(EndShowSelector, null);
            }
#endif
        }
        private void EndShowSelector(IAsyncResult ar)
        {
#if !WINDOWS_PHONE
            try
            {
                StorageDevice = StorageDevice.EndShowSelector(ar);

                if (StorageDevice == null)
                {
                    UserChosenNotToUseStorage = true;
                    DeviceLoaded = false;
                    InActivity = false;
                    return;
                }
                FreeSpace_KBytes = StorageDevice.FreeSpace / 1024f;
                FreeSpace_MBytes = FreeSpace_KBytes / 1024f;
                FreeSpace_GBytes = FreeSpace_MBytes / 1024;
                DeviceLoaded = true;
                InActivity = false;
            }
            catch
            {
                UserChosenNotToUseStorage = true;
                DeviceLoaded = false;
                InActivity = false;
            }
#endif
        }

#if !WINDOWS_PHONE
        /// <summary>
        /// Storage Containers previously created
        /// </summary>
        protected List<StorageContainer> Containers = new List<StorageContainer>();
        public StorageContainer GetStorageContainer(string displayName)
        {
            if (DeviceLoaded)
            {
                try
                {
                    DisableAllContainers();

                    StorageDevice Device = this.StorageDevice;
                    IAsyncResult res = Device.BeginOpenContainer(displayName, null, null);

                    while (!res.IsCompleted)
                    {
                        // Wait until the Storage Container is loaded
                    }

                    StorageContainer container = Device.EndOpenContainer(res);
                    Containers.Add(container);
                    return container;
                }
                catch
                {
                }
            }

            return null;
        }
        public void DisableAllContainers()
        {
            for (int i = 0; i < Containers.Count; i++)
            {
                StorageContainer con = Containers[i];

                if (!con.IsDisposed)
                    con.Dispose();
            }
            Containers.Clear();
        }
#endif

        public void Reset()
        {
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }
    }
}
